package com.jxd.dianping.controller;

import com.jxd.dianping.common.BusinessException;
import com.jxd.dianping.common.CommonRes;
import com.jxd.dianping.common.CommonUtil;
import com.jxd.dianping.common.EmBusinessError;
import com.jxd.dianping.model.UserModel;
import com.jxd.dianping.request.LoginReq;
import com.jxd.dianping.request.RegisterReq;
import com.jxd.dianping.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Objects;

@RestController
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private HttpServletRequest request;

    public static final String CURRENT_USER_SESSION_KEY = "currentUserSession";

    @GetMapping("/user/get")
    public CommonRes getUser(@RequestParam(name = "id") Integer id) throws BusinessException {
        UserModel user = userService.getUser(id);
        if (Objects.isNull(user)) {
            // return CommonRes.create(new CommonError(EmBusinessError.NO_OBJECT_FOUND), "fail");
            throw new BusinessException(EmBusinessError.NO_OBJECT_FOUND);
        }
        return CommonRes.create(user);
    }

    @PostMapping("/user/register")
    public CommonRes register(@Valid @RequestBody RegisterReq registerReq, BindingResult bindingResult) throws BusinessException, UnsupportedEncodingException, NoSuchAlgorithmException {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, CommonUtil.processErrorString(bindingResult));
        }
        UserModel registerUser = new UserModel();
        registerUser.setTelphone(registerReq.getTelphone());
        registerUser.setPassword(registerReq.getPassword());
        registerUser.setGender(registerReq.getGender());
        registerUser.setNickName(registerReq.getNickName());
        UserModel register = userService.register(registerUser);
        return CommonRes.create(register);
    }

    @PostMapping("/user/login")
    public CommonRes login(@Valid @RequestBody LoginReq loginReq, BindingResult bindingResult) throws BusinessException, UnsupportedEncodingException, NoSuchAlgorithmException {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, CommonUtil.processErrorString(bindingResult));
        }
        UserModel loginUser = new UserModel();
        loginUser.setPassword(loginReq.getPassword());
        loginUser.setTelphone(loginReq.getTelphone());
        UserModel login = userService.login(loginUser);
        // 存入session
        request.getSession().setAttribute(CURRENT_USER_SESSION_KEY, login);
        return CommonRes.create(login);
    }

    @PostMapping("/user/logout")
    public CommonRes logout() {
        // 使所有session无效
        request.getSession().invalidate();
        return CommonRes.create(null);
    }

    // 通过sessionId获取用户信息
    @PostMapping("/user/getCurrentUser")
    public CommonRes getCurrentUser() {
        UserModel userModel = (UserModel) request.getSession().getAttribute(CURRENT_USER_SESSION_KEY);
        return CommonRes.create(userModel);
    }

}
